package com.luccesit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.luccesit.entity.Book;
import com.luccesit.util.db.DBHelp;
import com.luccesit.util.db.Page;
import com.luccesit.util.db.RowMapper;


public class BookDao {

	private DBHelp<Book> db = new DBHelp<Book>();
	
	public void save(Book book) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO `t_book` ");
		sql.append("(`id`,`bookname`,`bookauthor`,`publishdate`,`pagenum`,`price`,`salesprice`,`content`,`authorcontent`,`preface`,`pic`,`booknum`,`dangenum`,`booktype_id`,`publish_id`) ");
		sql.append("VALUES");
		sql.append("(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		db.executeSQL(sql.toString(), book.getId(),book.getBookname(),book.getBookauthor(),book.getPublishdate(),book.getPagenum(),book.getPrice(),book.getSalesprice(),book.getContent(),book.getAuthorcontent(),book.getPreface(),book.getPic(),book.getBooknum(),book.getDangenum(),book.getBooktypeId(),book.getPublishId());
	}
	
	public void del(String id) {
		String sql = "delete from t_book where id = ?";
		db.executeSQL(sql, id);
	}
	
	public void update(Book book) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE `t_book` ");
		sql.append("SET `bookname` = ? ,`bookauthor` = ? ,`publishdate` = ? ,`pagenum` = ? ,`price` = ? ,`salesprice` = ? ,`content` = ? ,`authorcontent` = ? ,`preface` = ? ,`pic` = ? ,`booknum` = ? ,`dangenum` = ? ,`booktype_id` = ? ,`publish_id` = ?");
		sql.append("WHERE");
		sql.append("`id` = ?");
		db.executeSQL(sql.toString(), book.getBookname(),book.getBookauthor(),book.getPublishdate(),book.getPagenum(),book.getPrice(),book.getSalesprice(),book.getContent(),book.getAuthorcontent(),book.getPreface(),book.getPic(),book.getBooknum(),book.getDangenum(),book.getBooktypeId(),book.getPublishId(),book.getId());
	}
	
	public List<Book> findAll() {
		String sql = "select `id`,`bookname`,`bookauthor`,`publishdate`,`pagenum`,`price`,`salesprice`,`content`,`authorcontent`,`preface`,`pic`,`booknum`,`dangenum`,`booktype_id`,`publish_id`,createtime from t_book";
		return db.executeQueryToList(sql, new BookRowMapper());
	}
	
	public Book findById(String id) {
		String sql = "select `id`,`bookname`,`bookauthor`,`publishdate`,`pagenum`,`price`,`salesprice`,`content`,`authorcontent`,`preface`,`pic`,`booknum`,`dangenum`,`booktype_id`,`publish_id`,createtime from t_book where id = ?";
		return db.executeQueryToObject(sql, new BookRowMapper(), id);
	}
	public List<Book> findByBookTypeId(String typeid) {
		String sql = "select `id`,`bookname`,`bookauthor`,`publishdate`,`pagenum`,`price`,`salesprice`,`content`,`authorcontent`,`preface`,`pic`,`booknum`,`dangenum`,`booktype_id`,`publish_id`,createtime from t_book where booktype_id = ?";
		return db.executeQueryToList(sql, new BookRowMapper(), typeid);
	}
	
	
	
	public int count() {
		String sql = "select count(*) from t_book";
		return db.executeQueryToNumber(sql).intValue();
	}
	
	public Page<Book> findByPage(int pageNum) {
		String sql = "select `id`,`bookname`,`bookauthor`,`publishdate`,`pagenum`,`price`,`salesprice`,`content`,`authorcontent`,`preface`,`pic`,`booknum`,`dangenum`,`booktype_id`,`publish_id`,createtime from t_book limit ?,?";
		Page<Book> page = new Page<Book>(count(), pageNum);
		page.setList(db.executeQueryToList(sql, new BookRowMapper(), page.getStart(),page.getPageSize()));
		return page;
	}
	
	private class BookRowMapper implements RowMapper<Book> {

		public Book mapRow(ResultSet rs) throws SQLException {
			Book book = new Book();
			book.setAuthorcontent(rs.getString("authorcontent"));
			book.setBookauthor(rs.getString("bookauthor"));
			book.setBookname(rs.getString("bookname"));
			book.setBooknum(rs.getInt("booknum"));
			book.setBooktypeId(rs.getString("booktype_Id"));
			book.setContent(rs.getString("content"));
			book.setCreatetime(rs.getString("createtime"));
			book.setDangenum(rs.getInt("dangenum"));
			book.setId(rs.getString("id"));
			book.setPagenum(rs.getInt("pagenum"));
			book.setPic(rs.getString("pic"));
			book.setPreface(rs.getString("preface"));
			book.setPrice(rs.getFloat("price"));
			book.setPublishdate(rs.getString("publishdate"));
			book.setPublishId(rs.getString("publish_Id"));
			book.setSalesprice(rs.getFloat("salesprice"));
			return book;
		}
		
	}
}
